package com.sky.state.CM;

/**
 * @author : wushikai
 * <p>
 * date : 2022-03-28
 */

public class CandyMachineNormal {

    final static int soldOutState = 0; //初始状态
    final static int onReadyState = 1;  //待机状态
    final static int hasCoin = 2;  //准备状态
    final static int soldState = 3;  //售出状态

    private int state = soldOutState; //变量,用于存放当前的状态值
    private int count = 3; //糖果的数目

    public CandyMachineNormal(int count) {
        this.count = count;
        if (count > 0) {
            state = onReadyState;
        }
    }

    //投入硬币行为的时候，通过判断当前的状态来匹配所有的状态.
    public void insertCoin() {
        switch (state) {
            case soldOutState:
                System.out.println("you can't insert coin,the machine sold out!");
                break;
            case onReadyState: //只有在待机状态的时候,投入硬币行为正确,并将状态改变为准备状态
                state = hasCoin;
                System.out
                        .println("you have inserted a coin,next,please turn crank!");
                break;
            case hasCoin:
                System.out.println("you can't insert another coin!");

                break;
            case soldState:
                System.out.println("please wait!we are giving you a candy!");

                break;
        }

    }

    //回退硬币
    public void returnCoin() {
        switch (state) {
            case soldOutState:
                System.out
                        .println("you can't return,you haven't inserted a coin yet!");
                break;
            case onReadyState:
                System.out.println("you haven't inserted a coin yet!");
                break;
            case hasCoin:

                System.out.println("coin return!");
                state = onReadyState;

                break;
            case soldState:
                System.out.println("sorry,you already have turned the crank!");

                break;
        }

    }

    //转动曲柄
    public void turnCrank() {
        switch (state) {
            case soldOutState:
                System.out.println("you turned,but there are no candies!");
                break;
            case onReadyState:
                System.out.println("you turned,but you haven't inserted a coin!");
                break;
            case hasCoin:
                System.out.println("crank turn...!");
                state = soldState;
                dispense();
                break;
            case soldState:
                System.out
                        .println("we are giving you a candy,turning another get nothing,!");
                break;
        }

    }

    /**
     * dispense:
     * 分发
     * 英式:[dɪˈspens];美式:[dɪˈspens]
     *
     * 本地释义:
     * v. 发放，分配；提供，施予；配（药），发（药）
    * */
    private void dispense() {
        count = count - 1;
        System.out.println("a candy rolling out!");
        if (count > 0) {
            state = onReadyState;
        } else {
            System.out.println("Oo,out of candies");
            state = soldOutState;
        }

    }

    public void printState() {

        switch (state) {
            case soldOutState:
                System.out.println("***soldOutState***");
                break;
            case onReadyState:
                System.out.println("***onReadyState***");
                break;
            case hasCoin:

                System.out.println("***hasCoin***");

                break;
            case soldState:
                System.out.println("***soldState***");
                break;
        }

    }

    public static void main(String[] args){


        CandyMachineNormal candyMachine = new CandyMachineNormal(2);
        candyMachine.insertCoin();
        candyMachine.turnCrank();
        candyMachine.dispense();
    }
}
